//
//  SYCycleImages.m
//  16轮播图collection
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SYCycleImages.h"
@interface SYCycleImagesCell : UICollectionViewCell
@property (nonatomic,strong) UIImageView *cycleImage;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UILabel *titleLabel;
@end

@interface SYCycleImages ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong) UICollectionView *cycleCollectionView;
@property (nonatomic,strong) UIPageControl *pageControl;
@end

@implementation SYCycleImages

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
        
    }
    return self;
}

//stoaryboard走这个方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor blueColor];
        [self setUp];
        NSLog(@"%lf",self.frame.size.width);
    }
    return self;
}

- (void)setUp
{
    [self createCollectionView];
    [self createPageControl];
    
}

- (void)createCollectionView
{
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _cycleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _cycleCollectionView.delegate = self;
    _cycleCollectionView.dataSource = self;
//    _cycleCollectionView.frame = 
    
    _cycleCollectionView.pagingEnabled = YES;
    [_cycleCollectionView registerClass:[SYCycleImagesCell class] forCellWithReuseIdentifier:@"cell"];
    [self addSubview:_cycleCollectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SYCycleImagesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0f green:arc4random() % 256 /255.0f blue:arc4random() % 256 /255.0f alpha:1];
    cell.cycleImage.image = [UIImage imageNamed:_array[indexPath.row]];
    return cell;
}

- (void)createPageControl
{
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
     NSLog(@"%lf",self.frame.size.width);
    _cycleCollectionView.frame = self.bounds;
    _flowLayout.itemSize = self.bounds.size;
    [_cycleCollectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    
}

- (void)setArray:(NSArray *)array
{
    _array = array;
    [_cycleCollectionView reloadData];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    直接判断与0相等有何问题
    if (scrollView.contentOffset.x == 0) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width * (_array.count - 2), 0)];
       [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_array.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
        
    } else if (scrollView.contentOffset.x == _cycleCollectionView.bounds.size.width * (_array.count - 1))
    {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    
    
}
@end



@implementation SYCycleImagesCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _cycleImage = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_cycleImage];
    }
    return self;
}

@end

